/*
 * @: 
 * @Version: 1.0
 * @Autor: ziwei
 * @Date: 2021-06-21 15:01:07
 * @LastEditors: ziwei
 * @LastEditTime: 2022-01-24 15:58:56
 */
let Compiler = require('./Compiler');
//webpack --mode=development
function webpack(options) {
    //1.初始化参数：从配置文件和Shell文件中读取并合并参数，得出最终的配置对象
    //process.argv
    let shellOptions = process.argv.slice(2).reduce((shellOptions,item)=>{
        let [key,value] = item.split('=');
        shellOptions[key.slice(2)] = value
        return shellOptions
    },{});
    let finalConfig = {...options,...shellOptions}
    //2 用上一步得到的参数初始化Compiler对象
    let compiler = new Compiler(finalConfig);
    //3.加载所有配置的插件
    let {plugins} = finalConfig;
    for(let plugin of plugins){
        plugin.apply(compiler);
    }
    return compiler;
}

module.exports = webpack